package com.nenglai.admin.modular.report.service.upload;

import com.nenglai.admin.util.COSClientUtil;
import com.nenglai.model.report.entity.Template;
import com.qcloud.cos.model.ObjectMetadata;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

/**
 * @author kongdehui
 * @date 2021/07/02 10:44
 **/
@Component("cosUpload")
public class CosUpload implements Upload {

    @Resource
    COSClientUtil cosClientUtil;

    @Override
    public Object upload(MultipartFile file, String path, String filename) throws Exception {
        filename = path + "/" + filename;
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(file.getContentType());
        cosClientUtil.putObject(filename, file.getInputStream(), objectMetadata);
        return filename;
    }

    @Override
    public File download(Template template) {
        File file = new File("/tmp/" + template.getId());
        cosClientUtil.getObjectFile(template.getFilePath(), file);
        return file;
    }
}
